package impl

import (
	"context"
	"github.com/starmotioncf/soutchatinternal/constant"
	"github.com/starmotioncf/soutchatinternal/model"
	"github.com/starmotioncf/soutchatinternal/repository"
	"time"
)

type captchaRepository struct {
	*repository.Repository
}

func NewCaptchaRepository(r *repository.Repository) repository.CaptchaRepository {
	return &captchaRepository{
		Repository: r,
	}
}

// Create 创建一条短信记录
func (r *captchaRepository) Create(ctx context.Context, m *model.SmsCaptcha) error {
	return r.DB(ctx).Model(&model.SmsCaptcha{}).Create(m).Error
}

// GetByCodeId 通过验证码id找一条记录
func (r *captchaRepository) GetByCodeId(ctx context.Context, codeId string) (m *model.SmsCaptcha, err error) {
	err = r.DB(ctx).Where("code_id = ?", codeId).First(&m).Error
	return
}

// UpdateVerifyCode 校验更新验证码
func (r *captchaRepository) UpdateVerifyCode(ctx context.Context, id int64) error {
	return r.DB(ctx).Model(&model.SmsCaptcha{}).Where("id = ?", id).Updates(map[string]any{
		"status":    constant.SmsCaptchaStatusVerify,
		"verify_at": time.Now().Unix(),
	}).Error
}
